
var http = require('http');
var fs = require('fs');
var agents = require('./agents.js');
var common = require('./common.js');

function fileWalkDir(path, array)
{
    var files = fs.readdirSync(path+'/');
    for(var i in files) {
        var stats = fs.lstatSync(path+'/'+files[i]);
        if (stats.isDirectory()) {
            fileWalkDir(path+'/'+files[i], array);
        } else {
            array.push(path+'/'+files[i]);
        }
    }
}

function fileWalk()
{
    var r = [];
    fileWalkDir('.', r);
    var o = [];
    for(var i=0; i<r.length; i++) {
        if(r[i].substring(0, 12)=='./front-end/') continue;
        if(r[i].substring(0, 6)=='./log/') continue;
        o.push(r[i].substring(2));
    }
    return o;
}

var methods = {
    'exit' : function (arg, req, res) {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end('exit');
        process.exit(0);
    },
    'register' : function (arg, req, res) {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        var index = agents.add(req.client.remoteAddress + ':' + arg);
        res.end('done');
    },
    'unregister' : function (arg, req, res) {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        agents.del(req.client.remoteAddress + ':' + arg);
        res.end('done');
    },
    'list' : function (arg, req, res) {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end('{'+common.dumpvar(agents.list())+'}');
    },
    'filelist' : function (arg, req, res) {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end('{'+common.dumpvar(fileWalk())+'}');
    },
    'fileget' : function (arg, req, res) {
        if(arg=='') {
            res.writeHead(500, { 'Content-Type': 'text/plain' });
            res.end('invalid parameter' + req.url);
            return;
        }

        var files = fileWalk();
        for(var i=0; i<files.length; i++) {
            if(files[i]==arg) {
                fs.readFile(arg, function (error, content) {
                    if(error) {
                        res.writeHead(500);
                        res.end('error read ' + file);
                    } else {
                        res.writeHead(200, { 'Content-Type': 'text/html' });
                        res.end(content, 'utf-8');
                    }
                });
                return;
            }
        }
        res.writeHead(500);
        res.end('file not found.');
    },
    'rlog' : function (arg, req, res) {
        console.log(arg);
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end('done');
    },
    'runcase' : function (arg, req, res) {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end('done');
    },
};

var listen_ip = '0.0.0.0';
var listen_port = 8123;
var keepalive = 1000;

var args_methods = {
    'listen_ip' : function (v) {
        listen_ip = v;
    },
    'listen_port' : function (v) {
        listen_port = v;
    },
    'keepalive' : function (v) {
        keepalive = v;
    },
};

common.parseArgs();
common.processArgs(args_methods);

var httpd = http.createServer(function (req, res) {
    common.processHttpRequest(req, res, methods, true);
});

httpd.listen(listen_port, listen_ip);

console.log('webtest-master started at ' + listen_ip + ':' + listen_port);

setInterval(function() {
    agents.tick();
}, keepalive);
